package com.user.controller;

import com.user.service.UserService;
import com.user.entity.CommonResult;
import com.user.entity.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author zhangyan
 * @date 2022/10/4
 */
@RestController
@Slf4j
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 查询用户信息详情
     *
     * @param id id,
     * @return CommonResult<UserInfo>
     */
    @GetMapping("/user-info/{id}")
    public CommonResult<UserInfo> getUserInfo(@PathVariable Integer id) {
        log.debug("method enter param：{}", id);
        UserInfo userInfo = userService.findById(id);
        return CommonResult.success(userInfo);
    }

    /**
     * 查询用户列表
     *
     * @return CommonResult<List < UserInfo>>
     */
    @GetMapping("/user-info-list")
    public CommonResult<List<UserInfo>> getUserInfos() {
        log.info("Method of entering the query list");
        List<UserInfo> userInfos = userService.findAll();
        return CommonResult.success(userInfos);
    }

    /**
     * 添加用户
     *
     * @param userInfo user-info
     * @return CommonResult<UserInfo>
     */
    @PostMapping(value = "/user-info")
    public CommonResult<UserInfo> addUserInfo(@RequestBody @Valid UserInfo userInfo) {
        log.info("The method of adding a user is displayed:{}", userInfo);
        UserInfo info = userService.addUserInfo(userInfo);
        return CommonResult.success(info);

    }

    /**
     * 修改用户信息
     *
     * @param userInfo user-info
     * @return CommonResult<UserInfo>
     */
    @PutMapping(value = "/user-info")
    public CommonResult<UserInfo> updateUserInfo(@RequestBody @Valid UserInfo userInfo) {
        log.info("The method of update a user is displayed:{}", userInfo);
        UserInfo info = userService.addUserInfo(userInfo);
        return CommonResult.success(info);
    }

    /**
     * 删除用户信息
     *
     * @param id id
     * @return CommonResult<UserInfo>
     */
    @DeleteMapping(value = "/user-info/{id}")
    public CommonResult<UserInfo> deleteUserInfo(@PathVariable Integer id) {
        userService.deleteById(id);
        return CommonResult.success(null);
    }
}
